/*
 * @author Aldis Rameika
 * 
 * Implementation of "New tab" feature into tabber
 */

var tabIndex = 2;
var newTabIndex = 1;
var deletedTab = false;

function tabChanged(tabNum) {
    if (newTabIndex == tabNum) {
        addNewTab();
    } else {
        showTab(tabNum);
    }
}

function addNewTab() {
    // Creating new "+" tab
    var newTab = this.getElementsByClassName('tabbertab')[newTabIndex];
    var tabCopy = newTab.cloneNode(true);
    
    var tabber = this.getElementsByClassName('tabberlive')[0];
    tabber.appendChild(tabCopy);

    var tabberNav = this.getElementsByClassName('tabbernav')[0];
    var newTabLI = tabberNav.lastChild;
    var LICopy = newTabLI.cloneNode(true);
    
    // "+" tab
    LICopy.setAttribute("onClick", "showTab(" + (newTabIndex+1) + ")");
    tabberNav.appendChild(LICopy);
    
    // Creating new tab
    var tabberTab = this.getElementsByClassName('tabbertab');
    var newAppForm = document.getElementById('new_app_table_content');
    // Copying whole node so it won't be moved across tabs using deep cloning
    var appFormCopy = newAppForm.cloneNode(true);
    tabberTab[newTabIndex].appendChild(appFormCopy);
    
    //Change names of form elements
    var inputs = appFormCopy.getElementsByTagName('input');
    for (i = 0; i < inputs.length; i++) {
        var new_measure = /(new_measure\[n)(\d)(\])/g.exec(inputs[i].name);
        var subdivision = /(subdivision\[n)(\d)(\].+)/g.exec(inputs[i].name);
        var administrated = /(administrated\[n)(\d)(\].+)/g.exec(inputs[i].name);
        if (new_measure != null) {
            inputs[i].name = new_measure[1] + newTabIndex + new_measure[3];
            inputs[i].value = "Nauja " + (tabIndex++);
            inputs[i].title = "Naujos paraiškos pavadinimas";
            inputs[i].setAttribute("onkeyup", "tab_name_changed(" + newTabIndex + ")")
            add_manual_tooltip(inputs[i]);
        } else if (subdivision != null) {
            inputs[i].name = subdivision[1] + newTabIndex + subdivision[3];
            inputs[i].title = "Paraiškos apdorojimo laikas padalnyje valandomis pvz: 0.5";
            inputs[i].value = "";
            add_manual_tooltip(inputs[i]);
        } else if (administrated != null) {
            inputs[i].name = administrated[1] + newTabIndex + administrated[3];
            inputs[i].title = "Suadminstruotų paraiškų skaičius";
            inputs[i].value = "";
            add_manual_tooltip(inputs[i]);
        }
    }
    
    newTabIndex++;
    showTab(newTabIndex-1);
}

function showTab(tabToShow) {
    if (!deletedTab) {
        if (tabToShow == newTabIndex) {
            addNewTab();
            showTab(tabToShow);
        } else {
            var tabControl = this.getElementsByClassName('tabbernav')[0];
            var tabControlChilds = tabControl.childNodes;
            for (var i = 0; i < newTabIndex; i++) {
                if (i == tabToShow) {
                    tabControlChilds[i].className = "tabberactive";
                //var tabControlHeader = tabControlChilds[i].childNodes[0];
                //var name = "Nauja " + (i+1);
                //tabControlHeader.setAttribute("title", name);
                } else {
                    tabControlChilds[i].className = "";
                }
                tab_name_changed(i);
            }
            
            var tabs = this.getElementsByClassName('tabbertab');
            for (i = 0; i < newTabIndex; i++) {
                if (i == tabToShow) {
                    tabs[i].className = "tabbertab";
                    var tabHeader = tabs[i].childNodes[1];
                    tabHeader.innerHTML = "Nauja paraiška*" + newTabIndex;
                } else {
                    tabs[i].className = "tabbertab tabbertabhide";
                }
            }
            var tabControl1 = this.getElementsByClassName('tabbernav')[0];
            var tabControlChilds1 = tabControl1.childNodes;
            if (newTabIndex != 1) {
                for (i = 0; i < newTabIndex; i++) {
                    var closeButton = createCloseButton();
                    closeButton.setAttribute("onclick", "closeTab(" + i + ")")
                    var aTag = tabControlChilds1[i].childNodes[0];
                    if (aTag.childNodes[1] == null) {
                        aTag.appendChild(closeButton);
                    }
                }
            }
        }
    } else {
        deletedTab = false;
    }
}

function closeTab(tabNumber) {
    if (tabNumber-1 >= 0) {
        showTab(tabNumber-1);
    } else {
        showTab(0);
    }   

    // Removing tab
    var tabberNav = this.getElementsByClassName('tabbernav')[0];
    var tab = tabberNav.childNodes[tabNumber];
    tabberNav.removeChild(tab);
    
    // Removing tab content
    var tabToClose = this.getElementsByClassName('tabbertab')[tabNumber];
    var tabContainer = tabToClose.parentNode;
    tabContainer.removeChild(tabToClose);
    
    var tabControl = this.getElementsByClassName('tabbernav')[0];
    var tabControlChilds = tabControl.childNodes;
    for (var i = 0; i < newTabIndex; i++) {
        if (i == tabNumber-1) {
            tabControlChilds[i].className = "tabberactive";
        } else {
            tabControlChilds[i].className = "";
        }
        if (tabNumber-1 < 0) { 
            tabControlChilds[0].className = "tabberactive";
        }
        tabControlChilds[i].setAttribute("onclick", "showTab(" + i + ")");
    }
    
    var tabs = this.getElementsByClassName('tabbertab');
    for (i = 0; i < newTabIndex-1; i++) {
        if (i == tabNumber-1) {
            tabs[i].className = "tabbertab";
        } else {
            tabs[i].className = "tabbertab tabbertabhide";
        }
        if (tabNumber-1 < 0) { 
            tabs[0].className = "tabbertab";
        }
    }
   
    var closeButtons = this.getElementsByClassName("closeTab");
    for (i = 0; i < newTabIndex-1; i++) {
        closeButtons[i].setAttribute("onclick", "closeTab(" + i + ")")
    }
    
    var appInputs = this.getElementsByClassName("app_input");
    for (i = 0; i < newTabIndex-1; i++) {
        appInputs[i].childNodes[0].setAttribute("onkeyup", "tab_name_changed(" + i + ")")
    }
    
    deletedTab = true;
    newTabIndex--;
   
    if (newTabIndex == 1) {
        var closeImg = this.getElementsByClassName("closeTab")[0];
        var closeTab = closeImg.parentNode;
        closeTab.removeChild(closeImg);
    }  
}

function tab_name_changed(tabNumber) {
    var tabControl = this.getElementsByClassName('tabbernav')[0];
    var tabControlChilds = tabControl.childNodes;
    for (var i = 0; i < newTabIndex; i++) {
        if (i == tabNumber) {
            var appInputs = getElementsByClassName("app_input")
            var inputField = appInputs[i].childNodes[0];
            var tabControlHeader = tabControlChilds[i].childNodes[0];
            if (inputField.value) {
                tabControlHeader.innerHTML = inputField.value;
            } else {
                tabControlHeader.innerHTML = "Nauja " + (i+1);
            }
        }
    }
    
    var tabControl1 = this.getElementsByClassName('tabbernav')[0];
    var tabControlChilds1 = tabControl1.childNodes;
    if (newTabIndex != 1) {
        for (i = 0; i < newTabIndex; i++) {
            var closeButton = createCloseButton();
            closeButton.setAttribute("onclick", "closeTab(" + i + ")")
            var aTag = tabControlChilds1[i].childNodes[0];
            if (aTag.childNodes[1] == null) {
                aTag.appendChild(closeButton);
            }
        }
    }
}

function createCloseButton() {
    var closeButton = document.createElement("img");
    closeButton.setAttribute("class", "closeTab")
    closeButton.setAttribute("src", "GUI/Design/Images/x.gif");
    closeButton.setAttribute("alt", "[X]");
    closeButton.style.width = '8px';
    closeButton.style.height = '8px';
    closeButton.onmouseover = function() {
        this.style.border = '1px solid red'
    };
    closeButton.onmouseout = function() {
        this.style.border = '1px solid blue'
    }
    return closeButton;
}

function setNewTabIndex(newIndex) {
    newTabIndex = newIndex;
}

function setTabIndex(newIndex) {
    tabIndex = newIndex;
}

// For IE compatibility, because IE doesn't have this method
function getElementsByClassName(className) {
    var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
    var allElements = document.getElementsByTagName("*");
    var results = [];

    var element;
    for (var i = 0; (element = allElements[i]) != null; i++) {
        var elementClass = element.className;
        if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
            results.push(element);
    }

    return results;
}